//@target illustrator
//@include '../lib/j.js'

var JETTA = {}
JETTA.RGBColor = {
  'jetta blue': [40, 90, 255],
  'eosin': [255, 20, 60],
  'iron gray': [45, 62, 78],
  'Sky blue': [153, 212, 255],
  'silver gray': [189, 194, 195]
}

JETTA.CMYKColor = {
  'jetta blue': [90, 55, 0, 0],
  'eosin': [0, 95, 75, 0],
  'iron gray': [40, 15, 0, 75],
  'Sky blue': [35, 5, 0, 0],
  'silver gray': [0, 0, 0, 25]
}

JETTA.setTitle = function (textFrame) {
  CC.setParagraphAttr(textFrame, {
    fontIndex: CC.getFontIndex('HYQiHeiX3-GES'),
    tracking: 120,
    leading: 1.5,
  })
  CC.setCharacterAttr(textFrame, {
    fontIndex: CC.getFontIndex('PFDINTextCondPro-Medium'),
    scale: 1.1,
    tracking: 80,
    baselineShift: 0.04,
  })
}
JETTA.setSubtitle = function (textFrameRef) {
  CC.setParagraphAttr(textFrameRef, {
    fontIndex: CC.getFontIndex('HYQiHei-FES'),
    tracking: 50,
    leading: 1.5,
  })
  CC.setCharacterAttr(textFrameRef, {
    fontIndex: CC.getFontIndex('PFDinTextUniversal-Regular'),
    scale: 1.1,
    baselineShift: 0.03,
  })
}


JETTA.PC = {
  setLogoGroup: function (logoGroup, artboard, ratios) {
    logoRatio = logoGroup.width / logoGroup.height
    var rule = this.getPcRule(artboard, ratios)
    logoGroup.left = rule.x + rule.distance
    logoGroup.top = rule.y - rule.distance
    logoGroup.height = rule.logoHeight
    // logoGroup.width = rule.logoWidth
    logoGroup.width = rule.logoHeight * logoRatio


  },
  setVerticalMixLogo: function (logoGroup, artboard, ratios) {
    logoRatio = logoGroup.width / logoGroup.height
    var rule = this.getPcRule(artboard, ratios)
    logoGroup.left = rule.x + rule.distance
    logoGroup.top = rule.y - rule.distance
    logoGroup.width = rule.logoWidth
    logoGroup.height = rule.logoWidth / logoRatio
  },
  setUniLogoGroup: function (logoGroup, artboard, ratios) {
    var rule = this.getPcRule(artboard, ratios)
    var ratio = logoGroup.width / logoGroup.height
    logoGroup.left = rule.x + rule.distance
    logoGroup.top = rule.y - rule.distance
    logoGroup.height = rule.logoHeight
    logoGroup.width = rule.logoHeight * ratio
  },
  setBannerLogoGroup: function (logoGroup, artboard, ratios) {
    var logoGroupRatio = logoGroup.width / logoGroup.height
    var rule = this.getPcRule(artboard, ratios)
    logoGroup.left = rule.x + rule.distance
    logoGroup.top = rule.y - rule.height + rule.logoHalf + rule.logoHeight
    logoGroup.height = rule.logoHeight
    logoGroup.width = rule.logoHeight * logoGroupRatio

  },
  setVwLogoGroup: function (vwLogoGroup, artboard, ratios) {
    vwLogoGroupRatio = vwLogoGroup.width / vwLogoGroup.height
    var rule = this.getPcRule(artboard, ratios)
    vwLogoGroup.height = rule.vwHeight
    vwLogoGroup.top = rule.y - rule.height + rule.vwHeight  + (rule.distance / 2)
    vwLogoGroup.width = rule.vwHeight * vwLogoGroupRatio
    vwLogoGroup.left = rule.x + rule.width - vwLogoGroup.width - rule.distance
  },
 
  setBannerVwLogoGroup: function (vwLogoGroup, artboard, ratios) {
    var vwLogoGroupRatio = vwLogoGroup.width / vwLogoGroup.height
    var rule = this.getPcRule(artboard, ratios)
    // vwLogoGroup.left = rule.x + rule.distance
    vwLogoGroup.height = rule.vwHeight
    vwLogoGroup.width = rule.vwHeight * vwLogoGroupRatio

    vwLogoGroup.top = rule.y - rule.height + rule.vwHeight  + rule.vwHalf
    vwLogoGroup.left = rule.x + rule.width - rule.distance - vwLogoGroup.width

  },
  setSlogan: function (slogan, artboard, ratios) {
    var rule = this.getPcRule(artboard, ratios)
    slogan.left = rule.x + rule.distance
    slogan.top = rule.y - rule.height + rule.sloganHeight  + (rule.distance / 2) - rule.logoWidth * 0.017
    slogan.width = rule.logoWidth
    slogan.height = rule.sloganHeight
  },
  setQrcode: function (qrcode, artboard, ratios) {
    var rule = this.getPcRule(artboard, ratios)
    qrcode.left = rule.x + rule.width - rule.vwWidth- rule.qrWidth - (rule.distance * 1.5)
    qrcode.top = rule.y - rule.height + rule.qrHeight  + (rule.distance / 2)
    qrcode.width = rule.qrWidth
    qrcode.height = rule.qrHeight
  },
  setTitle: function (artboard, title) {
    var rule = this.getPcRule(artboard)
    if (!title) { return }
    pointText = app.activeDocument.textFrames.add()
    pointText.contents = title || ''
    var scale = (rule.width - rule.distance * 2) / pointText.width * 100
    pointText.resize(scale, scale)
    JETTA.setTitle(pointText)
    pointText.top = rule.y - rule.distance * 2 - rule.logoHeight
    pointText.left = rule.x + rule.distance
  },
  setSubtitle: function (artboard, title, subtitle) {
    var rule = this.getPcRule(artboard)
    if (!subtitle) { return }

    var titleText = app.activeDocument.textFrames.add()
    titleText.contents = title || ''
    JETTA.setTitle(titleText)
    var titleScale = (rule.width - rule.distance * 2) / titleText.width * 100
    titleText.resize(titleScale, titleScale)
    var titleTextHeight = titleText.height
    titleText.top = rule.y - rule.distance * 2 - rule.logoHeight
    titleText.left = rule.x + rule.distance
    titleText.remove()

    var subtitleText = app.activeDocument.textFrames.add()
    subtitleText.contents = subtitle || ''
    var scale = (rule.width - rule.distance * 2) / subtitleText.width * 80
    subtitleText.resize(scale, scale)
    JETTA.setSubtitle(subtitleText)
    subtitleText.top = rule.y - rule.logoHeight - rule.distance * 2 - titleTextHeight * 1.2
    subtitleText.left = rule.x + rule.distance
  },
  getPcRule: function(artboard, ratios) {
    ratios = ratios || [0, 0]
    var artboardSize = CC.getArtboardSize(artboard || ENV.currArtboard)
    var logoGroupScaleRatio = ratios[0] !== 0 ? ratios[0] : this.getPcLogoRatio(artboardSize)
    var vwLogoGroupScaleRatio = ratios[1] !== 0 ? ratios[1] : this.getPcVwLogoRatio(artboardSize)
    var logoGroupHeight = artboardSize.thin * logoGroupScaleRatio
    var logoGroupWidth = logoGroupHeight * 1.315811
    var vwHeight = vwLogoGroupScaleRatio * logoGroupHeight
    var vwWidth = vwHeight * 1.2627252
    var distance = logoGroupHeight * 0.526314
    var logoHalf = (artboardSize.height - logoGroupHeight) / 2
    var vwHalf = (artboardSize.height - vwHeight) / 2
    return {
      x: artboardSize.x,
      y: artboardSize.y,
      thin: artboardSize.thin,
      wide: artboardSize.wide,
      width: artboardSize.width,
      height: artboardSize.height,
      distance: distance,
      logoHalf: logoHalf,
      vwHalf: vwHalf,
      logoWidth: logoGroupWidth,
      logoHeight: logoGroupHeight,
      vwHeight: vwHeight,
      vwWidth: vwWidth,
      qrHeight: vwWidth,
      qrWidth: vwWidth * 0.8142,
      sloganHeight: logoGroupWidth * 0.1527308,
    }
  },
  getPcLogoRatio: function (size) {
    var ratioRange =  [1,    4/3,  7/5,  3/2,  16/9, 2,   3,   5,   9,   10];
    var logoRatios = [[0.4,  0.4,  0.4,  0.4,  0.4,  0.4, 0.4, 0.4, 0.4, 0.4],
                      [0.15, 0.15, 0.15, 0.15, 0.15, 0.2, 0.2, 0.3, 0.3, 0.4],
                      [0.15, 0.15, 0.15, 0.15, 0.15, 0.2, 0.2, 0.3, 0.3, 0.4]]
    var W = size.width || 1;
    var H = size.height || 1;
    var rectRatio = W > H ? W / H : H / W;
    var ratioIndex = JS.getArrIndex(ratioRange, rectRatio) - 1
    var typesRange = [130, 560]
    var shortSide = W < H ? W : H;
    var typeIndex = JS.getArrIndex(typesRange, shortSide)
    return logoRatios[typeIndex][ratioIndex]
  },
  getMobLogoRatio: function (size, type) {
    var ratioRange = {
      S: [3/1, 2/1, 4/3, 1/1, 3/4, 2/3, 1/2, 1/3],
      M: [3/1, 2/1, 4/3, 1/1, 3/4, 2/3, 1/2, 1/3],
      L: [4/1, 3/1, 2/1, 4/3, 1/1, 3/4, 2/3, 1/2],
    }
    var logoRatio = {
      S: [0.08, 0.08, 0.08, 0.08,  0.1,  0.1, 0.1,   0.1],
      M: [0.08, 0.08,  0.1,  0.1, 0.12, 0.12, 0.12, 0.12],
      L: [0.08, 0.08, 0.08,  0.1,  0.1, 0.12, 0.12, 0.12],
    }
    var W = size.width || 1;
    var H = size.height || 1;
    var rectRatio = W / H
    var ratioIndex = JS.getArrIndex(ratioRange, rectRatio) - 1
    return logoRatios[type][ratioIndex]
  },
  getPcVwLogoRatio: function (size) {
    return size.thin < 240 ? 0.8 : 0.6
  },
  getMobVwLogoRatio: function (size) {
    return size.thin < 240 ? 0.8 : 0.6
  },
}

//==============================================================================
var AI = {}

AI.drawCurrBackground = function (color, artboard) {
  CC.fix1px(artboard)
  var size = CC.getArtboardSize(artboard)
  var layer = CC.getLayer('background')
  layer.zOrder(ZOrderMethod.SENDTOBACK);
  var bgRect = layer.pathItems.rectangle(size.y, size.x, size.width, size.height);
  bgRect.fillColor = CC.createColor(CC.colorFilter(color))
  bgRect.stroked = false;
}

AI.putJettaLayer = function (layerName, order) {
  var layer = CC.getLayer(layerName || 'format')
  if (order) {
    var ZOrder = {
      front: ZOrderMethod.BRINGTOFRONT,
      back: ZOrderMethod.SENDTOBACK,
    }
    layer.zOrder(ZOrder[order]);
  }
}

AI.pcPutJettaLogo = function (dest, artboard, ratios) {
  var logo = CC.getAiFile(dest)
  // JETTA.PC.setLogoGroup(logo, artboard, ratios)
  JETTA.PC.setVerticalMixLogo(logo, artboard, ratios)
}

AI.pcPutJettaSlogan = function (dest, artboard, ratios) {
  size = CC.getArtboardSize(artboard)
  if (size.height > 350) {
    var slogan = CC.getAiFile(dest)
    JETTA.PC.setSlogan(slogan, artboard, ratios)
  }
}

AI.pcPutJettaQrcode = function (dest, artboard, ratios) {
  var slogan = CC.getAiFile(dest)
  JETTA.PC.setQrcode(slogan, artboard, ratios)
}

AI.pcPutVwLogo = function (artboard, ratios) {
  var vwLogo = CC.insertSvg(vw30Svg)
  JETTA.PC.setVwLogoGroup(vwLogo, artboard, ratios)

  var pageItems = vwLogo.pageItems
  if (isLightBg) {
    pageItems[0].pathItems[0].fillColor = '31 91 80 0, 181 63 68'.toAiColor() // 30
    pageItems[1].fillColor = '0 0 0 65, 125 125 125'.toAiColor() // line
    pageItems[2].pathItems[0].fillColor = '100 65 0 45, 0 30 80'.toAiColor() // vw
    pageItems[3].pathItems[0].fillColor = '0 0 0 65, 125 125 125'.toAiColor() // type
  } else {
    var whiteColor = '0 0 0 0, #fff'.toAiColor()
    pageItems[0].pathItems[0].fillColor = whiteColor
    pageItems[1].fillColor = whiteColor
    pageItems[2].pathItems[0].fillColor = whiteColor
    pageItems[3].pathItems[0].fillColor = whiteColor
  }
  
}

AI.pcBannerPutJettaLogo = function (dest, artboard, ratios) {
  var jettaLogo = CC.getAiFile(dest)
  JETTA.PC.setBannerLogoGroup(jettaLogo, artboard, ratios)
  
}

AI.pcBannerPutVwLogo = function (artboard, ratios) {
  var vwLogo = CC.insertSvg(vw30Svg)
  JETTA.PC.setBannerVwLogoGroup(vwLogo, artboard, ratios)

  var pageItems = vwLogo.pageItems
  if (isLightBg) {
    pageItems[0].pathItems[0].fillColor = '31 91 80 0, 181 63 68'.toAiColor() // 30
    pageItems[1].fillColor = '0 0 0 65, 125 125 125'.toAiColor() // line
    pageItems[2].pathItems[0].fillColor = '100 65 0 45, 0 30 80'.toAiColor() // vw
    pageItems[3].pathItems[0].fillColor = '0 0 0 65, 125 125 125'.toAiColor() // type
  } else {
    var whiteColor = '0 0 0 0, #fff'.toAiColor()
    pageItems[0].pathItems[0].fillColor = whiteColor
    pageItems[1].fillColor = whiteColor
    pageItems[2].pathItems[0].fillColor = whiteColor
    pageItems[3].pathItems[0].fillColor = whiteColor
  }
}

AI.addJettaSwatch = function () {
  CC.createSwatchGroup('Jetta', ENV.isRGBSpace ? JETTA.RGBColor : JETTA.CMYKColor)
}


var artboards = app.activeDocument.artboards;

function putAllPcFormat(needQrcode, ratios) {
  for(var i=0; i<artboards.length; i++) {
    putPcFormat(artboards[i], needQrcode, ratios)
  }
}

function putPcFormat(artboard, needQrcode, ratios) {
  AI.putJettaLayer('format', 'front')
  AI.drawCurrBackground([[40, 90, 255], [90, 55, 0, 0]], artboard || ENV.currArtboard)
  var rule = JETTA.PC.getPcRule(artboard, [0.1, 0.5])
  var isBanner = (rule.thin < 150 &&  (rule.wide / rule.thin) >= 5)
  var path = '~/Code/adobe/AiScripts/jetta-element/'
  if (isBanner) {
    AI.pcBannerPutJettaLogo(path + 'logo-mix-vertical.svg', artboard, ratios)
    AI.pcBannerPutVwLogo(artboard, ratios)
  } else {
    AI.pcPutJettaLogo(path + 'logo-mix-vertical.svg', artboard, ratios)
    AI.pcPutJettaSlogan(path + 'slogan.svg', artboard, ratios)
    AI.pcPutVwLogo(artboard, ratios)
    if(needQrcode) {
      var qrcodeFilename = ENV.isRGBSpace ?  'qrcode.svg' : 'qrcode-cmyk.ai'
      AI.pcPutJettaQrcode(path + qrcodeFilename, artboard, ratios)
    }
  }
}

windowRes = "dialog \
{ text:'Jetta Format', \
  titleGroup: Group {\
    label1: StaticText { \
      text:'Mr  Title',\
      properties:{ multiline:false }\
    },\
    MRTitle: EditText {\
      text:'',\
      characters: 55,\
      properties:{multiline:false}\
    },\
    alignment:['left','top']\
  },\
  subtitleGroup: Group {\
    label1: StaticText { text:'Subtitle',\
      properties:{ multiline: false }\
    },\
    MRSubTitle: EditText { \
      text:'', characters: 55,\
      properties:{ multiline:false }\
    },\
    alignment:['left','top']\
  },\
  optGroup: Group {\
    label1: StaticText { text:'Ratios :',\
      properties:{multiline:false}\
    },\
    jettaRatio: EditText {\
      text:'',\
      characters: 4,\
      properties:{multiline:false}\
    },\
    vwRatio: EditText {\
      text:'',\
      characters: 3,\
      properties:{multiline:false}\
    },\
    checkArtboards: Checkbox { \
      text: 'All', \
      value: true, \
    }, \
    checkLight: Checkbox { \
      text: 'Light', \
      value: false, \
    }, \
    checkMob: Checkbox { \
      text: 'Mob', \
      value: false, \
    }, \
    checkQrcode: Checkbox { \
      text: 'QR', \
      value: false, \
    }, \
    alignment:'right', \
    okBtn: Button { text:'Submit', },\
    cancelBtn: Button { text:'Cancel', }\
  }\
}"

var win = new Window(windowRes)

var MRTitle = ''
var ratios = [0, 0]
var MRSubTitle = ''
var isArtboardsChecked = true
var isQrcodeChecked = false
var isLightBg = false

win.optGroup.checkQrcode.onClick = function() {
  isQrcodeChecked = win.optGroup.checkQrcode.value
}

win.optGroup.checkArtboards.onClick = function() {
  isArtboardsChecked = win.optGroup.checkArtboards.value
}

win.optGroup.checkLight.onClick = function() {
  isLightBg = win.optGroup.checkLight.value
}

win.optGroup.checkMob.onClick = function() {
  isMob = win.optGroup.checkMob.value
  if (isMob) {
    win.optGroup.jettaRatio.text = '0.12'
    win.optGroup.vwRatio.text = '0.8'
  } else {
    win.optGroup.jettaRatio.text = ''
    win.optGroup.vwRatio.text = ''
  }
}

win.optGroup.okBtn.onClick = function() {
  MRTitle = win.titleGroup.MRTitle.text
  MRSubTitle = win.subtitleGroup.MRSubTitle.text
  ratios[0] = +win.optGroup.jettaRatio.text || 0
  ratios[1] = +win.optGroup.vwRatio.text || 0
  win.close();
  CC.renameDefaultLayer()
  AI.addJettaSwatch()
  if(MRTitle || MRSubTitle) {
    for(var i=0; i<artboards.length; i++) {
      if (MRTitle) {
        JETTA.PC.setTitle(artboards[i], MRTitle)
      }
      if (MRSubTitle) {
        JETTA.PC.setSubtitle(artboards[i], MRTitle, MRSubTitle)
      }
    }
  }
  if (isArtboardsChecked) {
    putAllPcFormat(isQrcodeChecked, ratios)
  } else {
    putPcFormat(ENV.currArtboard, isQrcodeChecked, ratios)
  }
}

win.optGroup.cancelBtn.onClick = function() {
  win.close();
}

win.show();
